import * as server from "@minecraft/server";
import * as native_tools from "../system/native_tools";
import * as derived_tools from "../system/derived_tools";
export function BlockUpdateAfterEvent(block) {
    const blocks = [];
    for (let axleX = -1; axleX <= 1; axleX++)
        for (let axleY = -1; axleY <= 1; axleY++)
            for (let axleZ = -1; axleZ <= 1; axleZ++) {
                const target = block.offset({ x: axleX, y: axleY, z: axleZ });
                if (target)
                    blocks.push(target);
            }
    ;
    blocks.forEach(block => {
        switch (block.typeId) {
            case 'block_cable:enable_control':
                EnableControl(block);
                break;
            case 'block_cable:basic_pipeline':
                BasicPipeline(block);
                break;
            case 'block_cable:pulse_latch':
                PulseLatch(block);
                break;
            case 'block_gate:portal_above':
                PortalGate(block);
                break;
            case 'block_gate:portal_below':
                PortalGate(block);
                break;
            case 'block_gate:andesite':
                PlaneGate(block);
                break;
            case 'block_gate:birch':
                PlaneGate(block);
                break;
            case 'block_gate:basalt':
                PlaneGate(block);
                break;
            case 'block_gate:diorite':
                VerticalGate(block);
                break;
            case 'block_gate:deepslate':
                VerticalGate(block);
                break;
            case 'block_gate:spruce':
                VerticalGate(block);
                break;
            case 'block_decorating:jungle_wood_chair':
                JungleWoodChair(block);
                break;
            case 'block_decorating:diorite_table':
                DioriteTable(block);
                break;
            default: break;
        }
    });
}
;
function BasicPipeline(object) {
    const direction_positive_x = object.east()?.hasTag('data_tag:magic_cable.port_negative.X') ?? false;
    const direction_negative_x = object.west()?.hasTag('data_tag:magic_cable.port_positive.X') ?? false;
    const direction_positive_y = object.above()?.hasTag('data_tag:magic_cable.port_negative.Y') ?? false;
    const direction_negative_y = object.below()?.hasTag('data_tag:magic_cable.port_positive.Y') ?? false;
    const direction_positive_z = object.south()?.hasTag('data_tag:magic_cable.port_negative.Z') ?? false;
    const direction_negative_z = object.north()?.hasTag('data_tag:magic_cable.port_positive.Z') ?? false;
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.X', direction_positive_x);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.X', direction_negative_x);
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Y', direction_positive_y);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Y', direction_negative_y);
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Z', direction_positive_z);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Z', direction_negative_z);
}
;
function PulseLatch(object) {
    const direction_positive_x = object.east()?.hasTag('data_tag:magic_cable.logic_negative.X') ? true : object.east()?.hasTag('data_tag:magic_cable.port_negative.X');
    const direction_negative_x = object.west()?.hasTag('data_tag:magic_cable.logic_positive.X') ? true : object.west()?.hasTag('data_tag:magic_cable.port_positive.X');
    const direction_positive_y = object.above()?.hasTag('data_tag:magic_cable.logic_negative.Y') ? true : object.above()?.hasTag('data_tag:magic_cable.port_negative.Y');
    const direction_negative_y = object.below()?.hasTag('data_tag:magic_cable.logic_positive.Y') ? true : object.below()?.hasTag('data_tag:magic_cable.port_positive.Y');
    const direction_positive_z = object.south()?.hasTag('data_tag:magic_cable.logic_negative.Z') ? true : object.south()?.hasTag('data_tag:magic_cable.port_negative.Z');
    const direction_negative_z = object.north()?.hasTag('data_tag:magic_cable.logic_positive.Z') ? true : object.north()?.hasTag('data_tag:magic_cable.port_positive.Z');
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.X', direction_positive_x ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.X', direction_negative_x ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Y', direction_positive_y ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Y', direction_negative_y ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_positive.Z', direction_positive_z ?? false);
    native_tools.TrySetPermutation(object, 'STATE:direction_negative.Z', direction_negative_z ?? false);
}
;
function EnableControl(object) {
    const output = [
        object.east(),
        object.west(),
        object.south(),
        object.north()
    ].filter(block => block?.hasTag('data_tag:magic_cable.series')).length;
    const control = [
        object.above(),
        object.below()
    ].filter(block => block?.hasTag('data_tag:magic_cable.latch')).length;
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:output', output != 0 ? 1 : 0), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:control', control != 0 ? 1 : 0), 2);
}
;
function PortalGate(object) {
    const direction_0 = object.west()?.hasTag('data_tag:magic_portal.series') ?? false;
    const direction_1 = object.south()?.hasTag('data_tag:magic_portal.series') ?? false;
    const direction_2 = object.east()?.hasTag('data_tag:magic_portal.series') ?? false;
    const direction_3 = object.north()?.hasTag('data_tag:magic_portal.series') ?? false;
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_0', direction_0 ? 1 : 0), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_1', direction_1 ? 1 : 0), 2);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_2', direction_2 ? 1 : 0), 3);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_3', direction_3 ? 1 : 0), 4);
}
;
function PlaneGate(object) {
    const direction_0 = object.east()?.typeId == object.typeId;
    const direction_1 = object.west()?.typeId == object.typeId;
    const direction_2 = object.south()?.typeId == object.typeId;
    const direction_3 = object.north()?.typeId == object.typeId;
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_0', direction_0 ? 1 : 0), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_1', direction_1 ? 1 : 0), 2);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_2', direction_2 ? 1 : 0), 3);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:direction_3', direction_3 ? 1 : 0), 4);
}
;
function VerticalGate(object) {
    let about = 0, center = 0;
    if (object.west()?.typeId == object.typeId)
        about = 1;
    else if (object.south()?.typeId == object.typeId)
        about = 2;
    else if (object.east()?.typeId == object.typeId)
        about = 3;
    else if (object.north()?.typeId == object.typeId)
        about = 4;
    if (object.east()?.typeId == object.typeId && object.west()?.typeId == object.typeId)
        center = 1;
    else if (object.south()?.typeId == object.typeId && object.north()?.typeId == object.typeId)
        center = 1;
    if (about == 0)
        return;
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:about', about), 1);
    server.system.runTimeout(() => native_tools.TrySetPermutation(object, 'STATE:center', center), 2);
}
;
function JungleWoodChair(object) {
    if (object.permutation.getState('STATE:stage') != 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.typeId != object.typeId && object.west()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;
            case 'north':
                if (object.east()?.typeId != object.typeId && object.west()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;
            case 'east':
                if (object.south()?.typeId != object.typeId && object.north()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;
            case 'west':
                if (object.south()?.typeId != object.typeId && object.north()?.typeId != object.typeId)
                    native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                break;
            default: break;
        }
    }
    else if (object.permutation.getState('STATE:stage') == 0 && object.permutation.getState('STATE:type') == 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.hasTag('data_tag:home_decorating.chair.south'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.west()?.hasTag('data_tag:home_decorating.chair.south'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;
            case 'north':
                if (object.east()?.hasTag('data_tag:home_decorating.chair.north'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.west()?.hasTag('data_tag:home_decorating.chair.north'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;
            case 'east':
                if (object.south()?.hasTag('data_tag:home_decorating.chair.east'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.north()?.hasTag('data_tag:home_decorating.chair.east'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;
            case 'west':
                if (object.south()?.hasTag('data_tag:home_decorating.chair.west'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.north()?.hasTag('data_tag:home_decorating.chair.west'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;
            default: break;
        }
    }
}
;
function DioriteTable(object) {
    if (object.permutation.getState('STATE:stage') != 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.hasTag('data_tag:home_decorating.table.south')) {
                    if (object.west()?.hasTag('data_tag:home_decorating.table.south'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.east()?.hasTag('data_tag:home_decorating.table.south')) {
                    if (!object.west()?.hasTag('data_tag:home_decorating.table.south'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                }
                ;
                break;
            case 'north':
                if (object.east()?.hasTag('data_tag:home_decorating.table.north')) {
                    if (object.west()?.hasTag('data_tag:home_decorating.table.north'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.east()?.hasTag('data_tag:home_decorating.table.north')) {
                    if (!object.west()?.hasTag('data_tag:home_decorating.table.north'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                }
                ;
                break;
            case 'east':
                if (object.south()?.hasTag('data_tag:home_decorating.table.east')) {
                    if (object.north()?.hasTag('data_tag:home_decorating.table.east'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.south()?.hasTag('data_tag:home_decorating.table.east')) {
                    if (!object.north()?.hasTag('data_tag:home_decorating.table.east'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                }
                ;
                break;
            case 'west':
                if (object.south()?.hasTag('data_tag:home_decorating.table.west')) {
                    if (object.north()?.hasTag('data_tag:home_decorating.table.west'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                }
                else if (!object.south()?.hasTag('data_tag:home_decorating.table.west')) {
                    if (!object.north()?.hasTag('data_tag:home_decorating.table.west'))
                        native_tools.TrySetPermutation(object, 'STATE:stage', 0);
                }
                ;
                break;
            default: break;
        }
    }
    else if (object.permutation.getState('STATE:stage') == 0) {
        switch (object.permutation.getState('minecraft:cardinal_direction')) {
            case 'south':
                if (object.east()?.hasTag('data_tag:home_decorating.table.south') && object.west()?.hasTag('data_tag:home_decorating.table.south'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.east()?.hasTag('data_tag:home_decorating.table.south'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.west()?.hasTag('data_tag:home_decorating.table.south'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;
            case 'north':
                if (object.east()?.hasTag('data_tag:home_decorating.table.north') && object.west()?.hasTag('data_tag:home_decorating.table.north'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.east()?.hasTag('data_tag:home_decorating.table.north'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.west()?.hasTag('data_tag:home_decorating.table.north'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;
            case 'east':
                if (object.south()?.hasTag('data_tag:home_decorating.table.east') && object.north()?.hasTag('data_tag:home_decorating.table.east'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.south()?.hasTag('data_tag:home_decorating.table.east'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                else if (object.north()?.hasTag('data_tag:home_decorating.table.east'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                break;
            case 'west':
                if (object.south()?.hasTag('data_tag:home_decorating.table.west') && object.north()?.hasTag('data_tag:home_decorating.table.west'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 3);
                else if (object.south()?.hasTag('data_tag:home_decorating.table.west'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 2);
                else if (object.north()?.hasTag('data_tag:home_decorating.table.west'))
                    native_tools.TrySetPermutation(object, 'STATE:stage', 1);
                break;
            default: break;
        }
        ;
        if (derived_tools.IsEnable(15))
            server.system.run(() => native_tools.TrySetPermutation(object, 'STATE:conceal', 1));
    }
}
;
